<?php session_start();
if (isset($_POST['value'])){
	$callfunction = $_POST['value'];
	list($callfunction, $parameter, $parameter2) = explode(";", $callfunction);
	$callfunction($parameter, $parameter2);
}
	
	function checkusername($username){
		include("../dbconnect.php");
		$query = sprintf("Select * from Person where Username ='%s'",
		$db->real_escape_string($username));
		$result = $db->query($query);
		$row = $result->fetch_assoc();
		if (isset($row['Username'])){
			echo "<img src='images/wrong.png' width='18' height='18'/> This username is already in use";
		}else{
			echo "<img src='images/right.png' width='18' height='18'/> This username is free yet";
		}
	}
	
	function CheckEmail($mail){
		if (check_email_address($mail) == true){
			echo "<img src='images/right.png' width='18' height='18'/> This e-mail adress is valid";
		}else{
			echo "<img src='images/wrong.png' width='18' height='18'/> This e-mail adress isn't valid";
		}
	}

	function check_email_address($email) {
	  // First, we check that there's one @ symbol, 
	  // and that the lengths are right.
	  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
		// Email invalid because wrong number of characters 
		// in one section or wrong number of @ symbols.
		return false;
	  }
	  // Split it into sections to make life easier
	  $email_array = explode("@", $email);
	  $local_array = explode(".", $email_array[0]);
	  for ($i = 0; $i < sizeof($local_array); $i++) {
		if
	(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
	?'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
	$local_array[$i])) {
		  return false;
		}
	  }
	  // Check if domain is IP. If not, 
	  // it should be valid domain name
	  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
		$domain_array = explode(".", $email_array[1]);
		if (sizeof($domain_array) < 2) {
			return false; // Not enough parts to domain
		}
		for ($i = 0; $i < sizeof($domain_array); $i++) {
		  if
	(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
	?([A-Za-z0-9]+))$",
	$domain_array[$i])) {
			return false;
		  }
		}
	  }
	  return true;
	}
	
	function Login($username,$password){	
		include("../dbconnect.php");
		$query = sprintf("Select * from Person where Username ='%s'",
		$db->real_escape_string($username));
		$result = $db->query($query);
		$row = $result->fetch_assoc();

		if (isset($row) && $row['PeId'] != 0){
			if (md5($password) != $row['Password']){												
				$_SESSION['logd'] = 0;
					echo "<img src='images/wrong.png' width='18' height='18'/> Password or username is incorrect";
					exit;
			} else {
				if ($row['OwnAccount'] != 0){
					$query2 = sprintf("Insert into Login (PeId) values ('%s')",
					$row['PeId']);
					$result2 = $db->query($query2);
					
					if ($row['Activated'] == 0){
						$_SESSION['id'] = $row['PeId'];
						$_SESSION['username'] = $row['Username'];
						$_SESSION['password'] = $row['Password'];
						echo "activate.php";
						exit;
					}
					
					$_SESSION['PeId'] = $row['PeId'];		
					$_SESSION['username'] = $row['Username'];
					$_SESSION['password'] = $row['Password'];
					$_SESSION['prename'] = $row['Firstname'];
					$_SESSION['name'] = $row['Name'];
					$_SESSION['logd'] = 1;
					echo "right";
				}else{
					echo "<img src='images/wrong.png' width='18' height='18'/> Password or username is incorrect";
				}
			}
		} else {
					echo "<img src='images/wrong.png' width='18' height='18'/> Password or username is incorrect";
		}
	}	
	
	function checkActivate($code){
		include("../dbconnect.php");
		$query = sprintf("Select * from Activation where ActivationCode ='%s'",
		$db->real_escape_string($code));
		$result = $db->query($query);
		$row = $result->fetch_assoc();
		if (isset($row) && $row['PeId'] == $_SESSION['id']){
			$query2 = sprintf("Select * from Person where PeId ='%s'",
			$_SESSION['id']);
			$result2 = $db->query($query2);
			$row2 = $result2->fetch_assoc();
			if ($_SESSION['password'] == $row2['Password']){
				$query3 = sprintf("Update Person set Activated = 1 where PeId ='%s'",
				$row2['PeId']);
				$db->query($query3);
				$_SESSION['PeId'] = $row2['PeId'];		
				$_SESSION['username'] = $row2['Username'];
				$_SESSION['password'] = $row2['Password'];
				$_SESSION['prename'] = $row2['Firstname'];
				$_SESSION['name'] = $row2['Name'];
				$_SESSION['logd'] = 1;
				echo "right";
			}else{
				echo "<img src='images/wrong.png' width='18' height='18'/> Password or username is incorrect";
			}
		}else{
			echo "<img src='images/wrong.png' width='18' height='18'/> Activationcode is incorrect";
		}
	}
	
	function sendMail($text, $Subject, $email){
		include("../mail/phpMailer_v2.1.0beta1/class.phpmailer.php");
		include("../mail/phpMailer_v2.1.0beta1/class.smtp.php");
		include("../mail/mailpw.php");

		$from = "hubmsg@gmail.com";
		$FromName = "MessageHub";

		$to = $email;
		$toName = "MsgHub Info";
		
		$HtmlBody = str_replace("\n","<br/>",$text);

		$TextBody = $text;

		$mail=new PHPMailer();

		$mail->IsSMTP();
		$mail->SMTPAuth   = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
		$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
		$mail->Port       = 465;                   // set the SMTP port 

		$mail->Username   = GUSER;  // GMAIL username
		$mail->Password   = GPWD;            // GMAIL password

		$mail->From       = $from;
		$mail->FromName   = $FromName;
		$mail->Subject    = $Subject;
		$mail->Body       = $HtmlBody;                      //HTML Body
		$mail->AltBody    = $TextBody; //Text Body

		$mail->WordWrap   = 50; // set word wrap

		$mail->AddAddress($to, $toName);
		$mail->AddReplyTo($from,$FromName);
		//$mail->AddAttachment("/path/to/file.zip");             // attachment
		//$mail->AddAttachment("/path/to/image.jpg", "new.jpg"); // attachment

		$mail->IsHTML(true); // send as HTML

		if(!$mail->Send()) {
		  echo "Mailer Error: " . $mail->ErrorInfo;
		} else {
		  echo "Message has been sent";
		}

	
	}
?>